layui.use(['jquery','form','layer','upload','laydate'],function () {
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer;
    var upload = layui.upload;
    var laydate = layui.laydate;

    var userIdd = localStorage.getItem("userId");
    var trueName = localStorage.getItem("trueName");
    var account = localStorage.getItem("account");
    var userName = localStorage.getItem("userName");

    laydate.render({
        elem:"#startDate",
        type:"datetime",
        tigger:"click"
    });

    laydate.render({
        elem:"#expireDate",
        type:"datetime",
        tigger:"click"
    });


    //上传头像
    var uploadInst = upload.render({
        elem: "#upload",
        url: "/upload/uploadFile",
        multiple: true,
        field: "files",
        size:"2M",
        accept: "images",
        before: function (obj) {
            $('#images').show();
        },
        done: function (res, index, upload) {
            var imgAddress = res.data.path;
            //console.log(res.data.path);
            $("#imgAddress").attr("src",imgAddress);
            $("#imgAddress").attr('style','width: 80px;height:80px;border:2px solid black');
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }/*, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }*/
    });

    //提交
    $("#tijiao").click(function () {
        var imgTitle = $("#imgTitle").val();
        var bannerName = $("#bannerName").val();
        var imgType = $("#imgType").val();
        var startDate = $("#startDate").val();
        var expireDate = $("#expireDate").val();
        var display = $("#display").val();
        var imgAddress = $("#imgAddress").attr('src');
        var describe = $("#describe").val();
        var remarks = $("#remarks").val();
        var updateDate = new Date();
        updateDate = getTime(updateDate);
       if(imgTitle!=null&&imgTitle!=""
           &&bannerName!=null&&bannerName!=""
           &&imgType!=null&&imgType!=""
           &&startDate!=null&&startDate!=""
           &&expireDate!=null&&expireDate!=""
           &&display!=null&&display!=""
           &&imgAddress!=null&&imgAddress!=""
           &&describe!=null&&describe!=""
           &&remarks!=null&&remarks!=""
       ){
        var json = {
            createId:userIdd,
            updateDate:updateDate,
            imgTitle:imgTitle,
            bannerName:bannerName,
            imgType:imgType,
            startDate:startDate,
            expireDate:expireDate,
            display:display,
            imgAddress:imgAddress,
            describe:describe,
            remarks:remarks
        };
        $.ajax({
            type:"post",
            dataType:"json",
            data:json,
            url:"/JsCarousel/addJsCarousel",
            success:function (data) {
                if(data){
                    layer.msg("添加成功");
                   /* parent.$("#quickHref").val('轮播图列表');
                    parent.getQuick();
                    return;*/
                }else{
                    layer.msg("添加失败");
                }
                location.reload();
            }
        })
       }else{
           layer.msg("请填写完整信息！！");
       }
    });
    
    
    //顶部时间
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    //时间的为零操作
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };
});